var ProductAdd = function() {

	var handleValidation = function() {

		var _url = window.location.pathname;
		var addFlag = _url.indexOf("add") != -1 ? true : false;

		var formAdd = $('#addForm');
		// var error = $('.alert-danger', formAdd);
		// var success = $('.alert-success', formAdd);

		var validator = formAdd.validate({
//			errorElement : 'span', // default input error message container
//			errorClass : 'help-block', // default input error message class
//			focusInvalid : false, // do not focus the last invalid input
//			ignore : "",
			rules : {
				productCode : {
					required : true,
					rangelength : [ 4, 20 ],
					remote: {
                        url: ctx + "/product/check",
                        type: "post",
                        dataType: "json",
                        data: {id : $("#productId").val()}
                    }
				},
				subFee : {
					required : true,
					number : true,
					range:[0,100]
				},
				productName : {
					required : true,
					rangelength : [ 4, 50 ],
					remote: {
                        url: ctx + "/product/check",
                        type: "post",
                        dataType: "json",
                        data: {id : $("#productId").val()}
                    }
				},
				openDate : {
					required : true,
					rangelength : [ 1, 64 ]
				},
				briefName : {
					required : true,
					rangelength : [ 4, 20 ],
					remote: {
                        url: ctx + "/product/check",
                        type: "post",
                        dataType: "json",
                        data: {id : $("#productId").val()}
                    }
				},
				redeemFee : {
					required : false,
					number : true,
					range:[0,100]
				},
				scale : {
					required : true,
					number : true,
					min:0
				},
				manageFee : {
					required : false,
					number : true,
					range:[0,100]
				},
				duration : {
					required : true,
					number : true,
					digits:true,
					min:0
				},
				recruitStartTime : {
					required : true,
					date : true
				},
				recruitEndTime : {
					required : true,
					date : true
				},
				fundRaising : {
					required : true
				},
				prospectiveEarnings : {
					required : true,
					rangelength : [ 1, 64 ]
				},
				underWriter : {
					required : true
				},
				fundManager : {
					required : true
				},
				fundInvest : {
					required : true,
					rangelength : [ 1, 512 ]
				},
				fundTrustee : {
					required : true
				},
				consultPhone : {
					required : true,
					rangelength : [ 5, 18 ]
				},
				subStart : {
					required : true,
					number : true,
					min:0
				},
				increaseAmount : {
					required : true,
					number : true,
					min:0
				},
				bankDeposit : {
					required : true
				},
				bankName : {
					required : true
				},
				bankAccount : {
					required : true,
					rangelength : [ 8, 50 ]
				},
				orders : {
					required : true,
					number : true
				},
				riskControl : {
					required : true,
					rangelength : [ 1, 1024 ]
				},
				remark : {
					required : false,
					rangelength : [ 1, 1024 ]
				},
				defaultDividend : {
					required : true,
					number : true,
					range:[0,100]
				},
				defaultCommission : {
					required : true,
					number : true,
					range:[0,100]
				},
				incomeType : {
					required : true
				},
				operationMode : {
					required : true
				},
				state : {
					required : true
				},
				productImage : {
					required : addFlag
				},
				productContactFile : {
					required : addFlag
				},
				handlingFee : {
					required : true,
					number : true,
					min:0
				},
				level : {
					required : true
				},
				investScope : {
					required : true,
					rangelength : [ 1, 1024 ]
				},
				contactPrefix : {
					required : true,
					rangelength : [ 1, 64 ]
				},
				trusteeFee : {
					required : false,
					number : true,
					range:[0,100]
				},
				setupTime : {
					required : false,
					date : true
				},
				riskDisclosureId : {
					required : true
				}
			},
            messages: {
            	productContactFile: {required:"请上传产品合同！"},
            	productCode: {remote:"产品代码已被使用！"},
            	productName: {remote:"产品全称已被使用！"},
            	briefName: {remote:"产品简称已被使用！"},
            	openDate:{ required:"开放式，此字段必填！"},
            	duration:{digits:"只能输入非负整数"},
            	scale:{min:"只能输入大于等于0的数"},
            	subStart:{min:"只能输入大于等于0的数"},
            	increaseAmount:{min:"只能输入大于等于0的数"}
            },
			success : function(label, element) {
				var icon = $(element).parent('.input-icon').children('i');
				$(element).closest('.form-group').removeClass('has-error')
						.addClass('has-success'); // set success class to the
			},
			submitHandler : function(form) {
			}
		});
	}

	return {
		init : function() {
			handleValidation();
		}
	};
}();

/*$('#addForm').ajaxForm(
		{
			dataType : 'json',
			success : function(data) {
				var message;
				if(data.code==1){
					message = "保存成功！";
				}else if(data.code == 5){
					message = "产品代码已存在！";
				}else if(data.code == 6){
					message = "产品简称已存在！";
				}else if(data.code == 7){
					message = "产品全称已存在！";
				}else if(data.code == 8){
					message = "招募开始时间/招募结束时间或产品状态错误！";
				}else if(data.code == 9){
					message = "开放式基金的开放日不能为空！";
				}else{
					message = "保存失败！";
				}
				bootbox.dialog({
					message : message,
					title : "温馨提示：",
					buttons : {
						main : {
							label : "确定",
							className : "btn btn-outline btn-primary",
							callback : function() {
								if (data.code == 1) {
									// window.location.reload();
									window.location.href = "product/edit/"
											+ data.data.id;
								}
							}
						}
					}
				});
			},
			error : function(msg) {
				bootbox.alert("保存产品出错。");
				// window.location.reload();
			}
		});*/

function checkOpenDate(){
	if($("#operationMode").val()==0 && $.trim($("#openDate").val()) == ""){
		 	$("#openDate").rules("add", { required:true,rangelength : [ 1, 64 ],messages:{ required:"开放式，此字段必填！"}}); 
 	}
	
	if($("#operationMode").val()==1){
 		$("#openDate").rules("add", { required:false,rangelength : [ 1, 64 ],messages:{ required:"开放式，此字段必填！"}}); 
 	}
}